/**
 * Created by dale on 2017/9/26.
 */
var resInList;

$(document).ready(function (){
    resInList= $('#resInList');
    $('.xh-datagrid-toolbar').parseToolbar();

    resInList.initDataGrid(baseUrl+'ims/res/getResIn').setSelectThenEnable(null,['bt_list_del','bt_list_editPrice','bt_list_addDesc']);

    $('#bt_list_search').click(function () {
        resInList.addDataGridParams(getParams());
    });

    $('#bt_list_add').click(function () {
        window.top.xhTop.showModal({
            title: '入库',
            content: '<iframe src="'+baseUrl+'ims/res/devInForm"  style="width: 100%;;border-style: none;padding: 0px;"  scrolling="yes"  height="250px;" ></iframe>',
            button:'提交'
        });
    });

    $('#bt_list_addScan').click(function () {
        window.top.xhTop.showModal({
            title: '扫码入库',
            content: '<iframe src="'+baseUrl+'ims/res/scanInForm"  style="width: 100%;;border-style: none;padding: 0px;"  scrolling="yes"  height="250px;" ></iframe>',
            button:'提交'
        });
    });

    $('#bt_list_addDesc').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id = resInList.getDataGridSelectedField('id');
        var flag = resInList.getDataGridSelectedField('flag');
        if (id == '') {
            window.top.xhTop.showError('错误','请先选择一个入库商品');
            return false;
        }
        window.top.swal({
            titleText: '添加备注',
            html:'<textarea type="date" class="form-control" id="mDesc" rows="3"></textarea>',
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonText: '确认',
            showLoaderOnConfirm: true,
            preConfirm: function () {
                return new Promise(function (resolve, reject) {
                    var desc = $('#mDesc' , window.top.document).val();
                    if (!desc) {
                        reject('备注必填！');
                    } else {
                        resolve(desc);
                    }
                })
            },
            allowOutsideClick: true
        }).then(function (desc) {
            xhExtra.ajaxRequest('ims/res/saveDesc',{id:id,flag:flag,sRemark:desc,type:'in'},null,function (res) {
                window.top.xhTop.showSuccess(res.msg);
                resInList.dataGridReload();
            });
        }, function(dismiss) {});

    });

    $('#bt_list_editPrice').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id = resInList.getDataGridSelectedField('id');
        var flag = resInList.getDataGridSelectedField('flag');
        var sProdName = resInList.getDataGridSelectedField('sProdName');
        if (id == '') {
            window.top.xhTop.showError('错误','请先选择一个入库商品');
            return false;
        }
        window.top.swal({
            titleText: sProdName,
            input: 'text',
            inputPlaceholder: '价格',
            inputAttributes: {class: 'form-control'},
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonText: '确认',
            showLoaderOnConfirm: true,
            preConfirm: function (price) {
                return new Promise(function (resolve, reject) {
                    if (!price || !/^[0-9]+$/.test(price) || price <= 0) {
                        reject('价格为必选项且必须为纯数字且大于0');
                    } else {
                        resolve();
                    }
                })
            },
            allowOutsideClick: true
        }).then(function (price) {
            xhExtra.ajaxRequest('ims/res/modifyPrice',{id:id,flag:flag,price:price,type:'in'},null,function (res) {
                window.top.xhTop.showSuccess(res.msg);
                resInList.dataGridReload();
            });
        }, function(dismiss) {});
    });


    $('#bt_list_del').click(function () {
        var id = resInList.getDataGridSelectedField('id');
        var flag = resInList.getDataGridSelectedField('flag');
        myWindow.initPopUp({ title: '确认删除',text: '你确定要删除这条记录吗?'},function () {
            $.ajax({
                url:baseUrl+'ims/res/delIn',
                data:{flag:flag,id:id},
                type:'post',
                success:function (data) {
                    window.top.swal('成功',data,'success');
                    resInList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal('失败',data.responseText,'error');
                }
            })
        });
    });


    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        if(rowData['flag']==='box' && rowData['sTypeName'] === '平板'){
            window.top.xhTop.showModal({
                title: '所包含的物品',
                content: '<iframe src="'+baseUrl+'ims/res/prodsInBox?iBoxId='+rowData['id']+'"  style="width: 100%;;border-style: none;padding: 0px;"  scrolling="yes"  height="450px;" ></iframe>',
                size:'lg',
            });
        }
    }

});

function getParams() {
    var key = xhExtra.getValue($('#list_search'));
    return {key:key};
}